× About Us How it works Pricing Student Archive Subjects Blog Contact Us

Enrich your knowledge with our informative blogs

What is a comment in programming?

Comments in programming means adding human readable descriptions in the source code explaining what the code intends to perform. 

Commenting is very essential part of every computer program as it helps in maintain the code in a better way extending its usability and scalability. 

It also plays a crucial role in debugging as the programmer will know the right place to debug and save a lot of time. 

Maintaining a well-documented code is an ideal quality of the programmer. 

How commenting works? 

Comments are not visible during the run time. A computer programmer can only see the comments in the source file.  

While executing the code, these comments are automatically hidden by the compiler, making only lines of code visible. 

Types of comments 

There are primarily two types of comments: 

  • Inline comments  

Here the comments are placed in line with the code. The comments take just one line or few words. 

Every language has it’s own syntax and symbols for commenting.  

Example1:  

Check the following C program for in line comment 

Int j= 10; // This is a comment in C Program. 

Two forward slashes are used to write a single line or in-line comment in C program. 

Example2: 

# This is comment in Python. 

The above example shows how single line comment is written in python language. 

  •   Nested comments  

If you want to explain what a function is doing or the role of a block of code, we need nested comments. 

As the explanation can be done in one line and we need multiple lines to explain the functionality.   

Here we need to provide beginning and ending of the comment. 

Let’s learn this with an example: 

In C language we start nested comments using ‘/*’ and end by ‘*/’. 

/* This function is used to 
calculate the average of numbers given by the user */ 

There are many languages such as Ada that do not support multiple line comment. You have to go for single line comments and split them in multiple lines using the in-line syntax for each line. 

What is a run-away comment? 

One of the main problem encountered by multi-line comments is that, programmers forget to close them. Look at this example: 

 /* set variables 

      x = 0; 

   /* set maximum size */ 

      max_size = 10; 

If you take a closer look, the line x=0; actually gets eaten up by the comment as the comment is not closed.  

The compiler will never able to find this line. Therefore, x=0 will never get executed. 

This is a kind of programming bug which is very difficult to locate and is known as run-away comment.  

This is the main reason most of the programmer prefer single-line comments or multiple (nested comments). 

Advantages of computer comments 

Computer program help and guide the programmer or the code-reviewer to: 

  • Understand the intent of code 
  • Debugging 
  • Including meta description  
  • Resource inclusion  
  • Insuring scalability of code and sometimes backward compatibility. 
  • Algorithm Description  

Where to use comments? 

As such, there is no hard and fast rule for comments inclusion. But there are certain places, where commenting is expected and is a must.  

  • Start of any program  

Here the intent of the program is explained such as who wrote the code, when, why, any modifications made etc. 

  • Above every function  

Before every function, it is important to describe what the function is doing and any algorithm the program is following. 

  • In-line comments  

Any line of code which is not usual or is tricky should be properly commented.  

It may happen that something that seems obvious to one person may not be the same for the other person.  

Read More – Coding and Programing Questions

View More – Useful links for Your Child’s Development 

Unveil your gateway to a lucrative career!
Unveil your gateway to a lucrative career!

Unleash the power of true logic building with Real-time instructions and live coding exposure.

Book A Demo Class

Tel Guru
Tel Guru

Register For The Demo Class

[footer-form]